package com.house.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.house.pojo.TUser;
import com.house.service.UserService;



@Controller

public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/user/index")
	public String indexPage(){
		
		return "home/index";
	}
	/**
	 * 登录页面
	 */
	@RequestMapping("/user/login")
	public String loginPage(){
		
		return "home/login";
	}
	@RequestMapping("/user/toLogin")
	public String toLogin(String username, String password, HttpSession session){
		try {
				List<TUser> loginUser = this.userService.userLogin(username, password);
				if(loginUser.size() != 1){
					session.setAttribute("error", "用户名密码错误");
					throw new RuntimeException();
				}			
				session.setAttribute("loginUser", loginUser.get(0));

				return "redirect:/user/index";
	
			}catch (Exception e) {
			    return "home/login";
		    }
	}
	
}
